package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.GameMapper;
import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.Game;
import com.kkb.pojo.GameExample;
import com.kkb.pojo.Team;
import com.kkb.pojo.TeamExample;
import com.kkb.vo.GameQueryVO;
import com.kkb.vo.QueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GameService {
    @Resource
    private GameMapper gameMapper;
    @Resource
    private TeamMapper teamMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Game> queryByPage(Integer pageNum, Integer pageSize, GameQueryVO vo) {
        GameExample gameExample = new GameExample();
        TeamExample teamExample = new TeamExample();
        GameExample.Criteria criteria = gameExample.createCriteria();
        TeamExample.Criteria criteria1 = teamExample.createCriteria();
        if (vo != null) {
            if (vo.getChineseName() != null && !"".equals(vo.getChineseName().trim())) {
                criteria1.andChineseNameLike("%" + vo.getChineseName() + "%");
                List<Team> teamList = teamMapper.selectByExample(teamExample);
                List<Integer> idList = new ArrayList<>();
                for (Team team : teamList) {
                    idList.add(team.getTeamId());
                }
                criteria.andHomeTeamIdIn(idList);
            }
            if (vo.getBeginDate() != null) {
                criteria.andGameDateGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate() != null) {
                criteria.andGameDateLessThanOrEqualTo(vo.getEndDate());
            }
            if (vo.getTypeId() != null && vo.getTypeId() != -1) {
                criteria.andTypeIdEqualTo(vo.getTypeId());
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Game> games = gameMapper.selectByExample(gameExample);
        for (Game game : games) {
            game.setHomeTeam(teamMapper.selectByPrimaryKey(game.getHomeTeamId()));
            game.setVisitingTeam(teamMapper.selectByPrimaryKey(game.getVisitingTeamId()));
        }
        return new PageInfo<>(games);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addGame(Game game) {
        Team homeTeam = teamMapper.selectByPrimaryKey(game.getHomeTeamId());
        Team visitingTeam = teamMapper.selectByPrimaryKey(game.getVisitingTeamId());
        if (homeTeam == null) {
            return -1;
        }
        if (visitingTeam == null) {
            return -2;
        }
        return gameMapper.insertSelective(game);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Game queryById(int gameId) {
        return gameMapper.selectByPrimaryKey(gameId);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateGame(Game game) {
        Team homeTeam = teamMapper.selectByPrimaryKey(game.getHomeTeamId());
        Team visitingTeam = teamMapper.selectByPrimaryKey(game.getVisitingTeamId());
        if (homeTeam == null) {
            return -1;
        }
        if (visitingTeam == null) {
            return -2;
        }
        return gameMapper.updateByPrimaryKeySelective(game);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteGame(int gameId) {
        Game game = gameMapper.selectByPrimaryKey(gameId);
        game.setIsDel(1);
        return gameMapper.updateByPrimaryKeySelective(game);
    }
}
